package team.stride.database.crud.tracks;

import java.util.ArrayList;
import java.util.List;

import team.stride.R;
import team.stride.database.StrideDB;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class BDTracksActivity extends Activity
{
	protected static final int MODIFY_DIALOG = 0;
	private List<TracksItem> recordsList;
	private ListView listLV;
	private TracksAdapter adapter;
	private Dialog modifDialog;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bd_tracks_layout);
		initDialog();
		listLV = (ListView) findViewById(R.id.tracksListView);

		recordsList = new ArrayList<TracksItem>();
		adapter = new TracksAdapter(this, recordsList);
		listLV.setAdapter(adapter);
	}

	@Override
	public void onResume()
	{
		super.onResume();
		recordsList.clear();
		Cursor cursor = managedQuery(StrideDB.CONTENT_URI_TRACKS, null, null, null, null);
		while (cursor.moveToNext())
		{
			final int id_index = cursor.getColumnIndex(StrideDB.ID_T);
			final int name_index = cursor.getColumnIndex(StrideDB.NAME_T);
			final int lenght_index = cursor.getColumnIndex(StrideDB.LENGTH_T);
			final int gpx_index = cursor.getColumnIndex(StrideDB.GPX_T);
			final int details_index = cursor.getColumnIndex(StrideDB.DETAILS_T);

			int id = cursor.getInt(id_index);
			String name = cursor.getString(name_index);
			int lenght = cursor.getInt(lenght_index);
			String gpx = cursor.getString(gpx_index);
			String details = cursor.getString(details_index);

			recordsList.add(new TracksItem(id, name, lenght, gpx, details));
		}
		cursor.close();
		adapter.notifyDataSetChanged();
	}

	private void initDialog()
	{
		modifDialog = new Dialog(this);
		modifDialog.setContentView(R.layout.bd_tracks_modify);
		modifDialog.setTitle("Modify a Track");
		modifDialog.setCancelable(true);
		modifDialog.setCanceledOnTouchOutside(true);
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		switch (id)
		{
		case MODIFY_DIALOG:
			return modifDialog;
		}
		return null;
	}

	public void showDialog(int modifyDialog, TracksItem entry)
	{
		refreshDialog(entry);
		showDialog(MODIFY_DIALOG);
	}

	private void refreshDialog(final TracksItem entry)
	{
		final EditText name = (EditText) modifDialog.findViewById(R.id.tracksModifyName);
		name.setText(entry.getName());
		final EditText gpx = (EditText) modifDialog.findViewById(R.id.tracksModifyGpx);
		gpx.setText(entry.getGpx());
		final EditText details = (EditText) modifDialog.findViewById(R.id.tracksModifyDetails);
		details.setText(entry.getDetails());

		Button okBtn = (Button) modifDialog.findViewById(R.id.tracksModifyOk);
		okBtn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				entry.setName(name.getText().toString());
				entry.setGpx(gpx.getText().toString());
				entry.setDetails(details.getText().toString());
				adapter.notifyDataSetChanged();

				final ContentResolver cr = v.getContext().getContentResolver();
				ContentValues values = new ContentValues();
				values.put(StrideDB.NAME_T, name.getText().toString());
				values.put(StrideDB.GPX_T, gpx.getText().toString());
				values.put(StrideDB.DETAILS_T, details.getText().toString());
				cr.update(StrideDB.CONTENT_URI_TRACKS, values, StrideDB.ID_T + " = " + entry.getId(), null);
				modifDialog.hide();
			}
		});

	}

}